class Solution(object):
    def candy(self, ratings):
        """
        :type ratings: List[int]
        :rtype: int
        """
        n = len(ratings)

        left = [0] * n
        left[0] = 1
        for i in range(n):
            if ratings[i-1] < ratings[i]:
                left[i] = left[i-1] + 1
            else:
                left[i] = 1

        right = [0] * n
        right[n-1] = 1
        for i in range(n-1, 0, -1):
            if ratings[i] < ratings[i-1]:
                right[i-1] = right[i] + 1
            else:
                right[i-1] = 1

        num = [0] * n
        Num = 0
        for i in range(n):
            num[i] = max(left[i], right[i])
            Num += num[i]

        return Num